package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.JwtTokenUtils;
import com.example.entity.Admin;
import com.example.entity.Audit;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.example.mapper.AuditMapper;
import com.example.service.AdminService;
import com.example.service.AuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> implements AuditService {

    @Autowired
    private AuditMapper auditMapper;


    @Override
    public List<Audit> getAudits() {
        return auditMapper.getUsers();
    }

    @Override
    public List<Audit> findBySearch(Params params) {
        return auditMapper.findBySearch(params);
    }

    @Override
    public void add(Audit audit) {
        //1. 非空判断
        if (StringUtils.isEmpty(audit.getName())){
            throw new CustomException("请假原因不能为空");
        }

        // 新增、编辑共用一个接口，因此做个判断
        // 新增的话，前端传入的对象中没有id
        // 编辑的话，前端传入的对象中有id
        if (audit.getId() == null){
            this.save(audit);
        }else {
            this.saveOrUpdate(audit);
        }
    }

    @Override
    public Audit findById(int id) {
        return this.getById(id);
    }
}
